package ru.evga.inse.server.gwt.admin.client.menu.dict.street;

import java.util.Collection;
import java.util.List;

import ru.evga.gwt.lib.common.client.domain.EntityKeyProvider;
import ru.evga.gwt.lib.common.client.ui.editor.IEntityEditor;
import ru.evga.inse.server.gwt.common.client.domain.dict.CityDto;
import ru.evga.inse.server.gwt.common.client.domain.dict.CityRenderer;
import ru.evga.inse.server.gwt.common.client.domain.dict.StreetDto;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.editor.client.EditorError;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ValueListBox;


/**
 * Edit view for StreetProxy.
 */
public class StreetEditPanel
    extends Composite
    implements IEntityEditor<StreetDto>
{
    interface Binder
        extends UiBinder<HTMLPanel, StreetEditPanel>
    {
    }
    
    private static final Binder BINDER = GWT.create(Binder.class);
    
    @UiField
    TextBox nameEditor;
    @UiField(provided = true)
    ValueListBox<CityDto> cityEditor = new ValueListBox<CityDto>(CityRenderer.instance(),
        new EntityKeyProvider<CityDto>());
    @UiField
    DivElement errors;

    public StreetEditPanel()
    {
        initWidget(BINDER.createAndBindUi(this));
    }

    public void showError(List<EditorError> errors)
    {
        SafeHtmlBuilder b = new SafeHtmlBuilder();
        for (EditorError error : errors)
        {
            b.appendEscaped(error.getPath()).appendEscaped(": ");
            b.appendEscaped(error.getMessage()).appendHtmlConstant("<br>");
        }
        this.errors.setInnerHTML(b.toSafeHtml().asString());
    }

    public void onServiceError(String serviceError)
    {
        SafeHtmlBuilder b = new SafeHtmlBuilder();
        b.appendEscaped(serviceError);
        this.errors.setInnerHTML(b.toSafeHtml().asString());
    }

  
    public void setCityPickerValues(Collection<CityDto> values)
    {
        cityEditor.setAcceptableValues(values);
    }
}
